<!-- 车辆列表 -->
<template>
  <div class="dashboard-container vehicle customer-list-box">
    <!-- 搜索区域 -->
    <el-card style="margin-top:20px">
      <el-form label-width="100px">
        <el-row
          type="flex"
          align="middle"
        >
          <el-col :span="8">
            <el-form-item label="车辆类型:">
              <el-select
                v-model="form.truckTypeId"
                clearable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in CarTypeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车牌号码:">
              <el-input
                v-model="form.licensePlate"
                clearable
                placeholder="请输入车牌号码"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button
                type="danger"
                @click="search"
              >搜索</el-button>
              <el-button @click="clear">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- tabs区域 -->
    <div class="tabs">
      <el-tabs
        v-model="activeName"
        type="card"
        @tab-click="tabClick"
      >
        <el-tab-pane
          :label="`全部 ${+CarTypeStatics[0] + +CarTypeStatics[1]}`"
          name="all"
        ></el-tab-pane>
        <el-tab-pane
          :label="`可用 ${+CarTypeStatics[1]}`"
          name="usable"
        ></el-tab-pane>
        <el-tab-pane
          :label="`停用 ${+CarTypeStatics[0]}`"
          name="disable"
        ></el-tab-pane>
      </el-tabs>
    </div>

    <!-- 表格区域 -->
    <el-card>
      <el-row>
        <el-button
          type="danger"
          style="margin-bottom:20px"
          @click="showAddDialog=true"
        > 新增车辆</el-button>
      </el-row>
      <el-table
        stripe
        :data="truckList"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="序号"
          width="60"
        >
        </el-table-column>
        <el-table-column
          prop="licensePlate"
          label="车牌号码"
          width="115"
        >
        </el-table-column>
        <el-table-column
          prop="truckTypeName"
          label="车辆类型"
          width="111"
        >
        </el-table-column>
        <el-table-column
          prop="driverNum"
          label="司机数量"
          width="111"
        >
        </el-table-column>
        <el-table-column
          label="车辆状态"
          width="111"
        >
          <template #default="{row}">
            <el-row
              type="flex"
              justify="start"
              align="middle"
            >
              <span
                v-if="!row.workStatus"
                style="display:inline-block;width:6px;height:6px;border-radius:50%;background:red;margin-right:5px"
              ></span>
              <span
                v-else
                style="display:inline-block;width:6px;height:6px;border-radius:50%;background:#1dc779;margin-right:5px"
              ></span>
              {{ row.workStatus===0?'停用':'可用' }}
            </el-row>

          </template>
        </el-table-column>
        <el-table-column
          prop="deviceGpsId"
          label="GPS设备ID"
          width="167"
        >
        </el-table-column>
        <el-table-column
          prop="allowableLoad"
          label="实载重量(T)"
          width="167"
        >
        </el-table-column>
        <el-table-column
          prop="allowableVolume"
          label="实载体积(方)"
          width="167"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          width="220"
          align="center"
        >
          <template #default="{row}">
            <el-link
              type="primary"
              :underline="false"
              @click="$router.push(`vehicle-detail?id=${row.id}&formUrlName=车辆管理`)"
            >查看详情</el-link>
            <el-link
              :type="row.workStatus===0?'primary':'danger'"
              style="border-right:1px solid #dee1e7;border-left:1px solid #dee1e7"
              :underline="false"
              @click="changeWorkStatus(row)"
            >{{ row.workStatus===0?'启用':'停用' }}</el-link>
            <el-link
              type="primary"
              :underline="false"
              @click="openDriver(row)"
            >配置司机</el-link>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="+counts"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>

    <!-- 新增车辆弹框 -->
    <VehicleAdd
      :show-add-dialog.sync="showAddDialog"
      @update="getTruckList"
    />

    <!-- 配置司机弹框 -->
    <arrangeDriver
      ref="arrangeDriver"
      :title-info="titleInfo"
      :car-modal-info="carModalInfo"
    />

  </div>
</template>
<script>
import { truckList, getCarTypeList, getCarTypeStatics, truckStatusUse, truckStatusNoUse } from '@/api/transit'
import VehicleAdd from './components/vehicle-add.vue'
import arrangeDriver from './components/arrange-driver.vue'
export default {
  name: 'Vehicle',
  components: {
    VehicleAdd,
    arrangeDriver
  },
  data() {
    return {
      page: 1,
      pageSize: 10,
      counts: 0,
      form: {
        truckTypeId: '', // 车辆类型
        licensePlate: '' // 车牌号码
      },
      truckList: [], // 车辆类型分页数据
      CarTypeList: [], // 车辆类型下拉数据
      activeName: 'all',
      CarTypeStatics: {
        0: '0',
        1: '0',
        35: '0'
      }, // 统计车辆
      workStatus: null, // 车辆状态
      showAddDialog: false, // 新增车辆编辑框
      titleInfo: {
        text: '配置司机'
      },
      carModalInfo: {}// row
    }
  },
  created() {
    this.getTruckList()
    this.getCarTypeList()
    this.getCarTypeStatics()
  },
  methods: {
    // 获取车辆类型分页数据
    async getTruckList() {
      const { data } = await truckList({
        page: this.page,
        pageSize: this.pageSize,
        truckTypeId: this.form.truckTypeId,
        licensePlate: this.form.licensePlate,
        workStatus: this.workStatus
      })
      // console.log(data)
      this.counts = data.counts
      this.truckList = data.items
    },
    // 获取车辆类型下拉
    async getCarTypeList() {
      const { data } = await getCarTypeList()
      // console.log(data)
      this.CarTypeList = data
    },
    // 获取统计车辆
    async getCarTypeStatics() {
      const { data } = await getCarTypeStatics()
      // console.log(data)
      this.CarTypeStatics = data
    },
    // 改变每页条数
    handleSizeChange(newPageSize) {
      this.pageSize = newPageSize
      this.getTruckList()
    },
    // 改变当前页
    handleCurrentChange(newPage) {
      this.page = newPage
      this.getTruckList()
    },
    // 搜索
    search() {
      this.page = 1
      this.getTruckList()
    },
    // 清除
    clear() {
      this.form = {
        truckTypeId: '', // 车辆类型
        licensePlate: '' // 车牌号码
      }
      this.page = 1
      this.getTruckList()
    },
    // tab切换时触发
    tabClick(tab, event) {
      // console.log(tab, event)
      this.page = 1
      if (tab._props.name === 'usable') {
        this.workStatus = 1
      } else if (tab._props.name === 'disable') {
        this.workStatus = 0
      } else {
        this.workStatus = null
      }
      this.getTruckList()
    },
    // 点击配置司机
    openDriver(row) {
      this.$confirm(`<p style="margin-bottom:10px">配置司机需满足以下条件：</p>
      <span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); border-radius: 50%; display: inline-block; text-align: center;  line-height: 14px;">1</span>
      <span style="margin-right:50px">车辆信息已完善</span>
      <span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); border-radius: 50%; display: inline-block; text-align: center;  line-height: 14px;">2</span>
      <span>车辆无未完成运输任务</span>`, '车辆配置', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true
      }).then(() => {
        this.carModalInfo = row
        // console.log(this.carModalInfo)
        // 调用弹层显示
        this.$refs.arrangeDriver.dialogFormV()
        // 获取绑定司机
        this.$refs.arrangeDriver.getDriverListByCar(row.id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消!'
        })
      })
    },
    // 点击启用/停用
    changeWorkStatus(row) {
      if (row.workStatus === 1) {
        this.$confirm(`<p style="margin-bottom:5px">确定要停用吗？车辆停用需满足以下条件：</p>
      <span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); border-radius: 50%; display: inline-block; text-align: center;  line-height: 14px;">1</span>
      <span style="margin-right:50px">车辆当前无运输任务</span>
      <span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); border-radius: 50%; display: inline-block; text-align: center;  line-height: 14px;">2</span>
      <span>车辆无关联运输路线</span>
      <p style="color:#ffbe28;margin-top:5px">注：停用后司机将自动解除绑定</p>`, '车辆停用', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          dangerouslyUseHTMLString: true
        }).then(async () => {
          const res = await truckStatusNoUse(row.id)
          // console.log(res)
          this.$message.error(res.msg)
        })
      } else {
        this.$confirm(`<p style="margin-bottom:5px">确定要启用吗？车辆启用需满足以下条件：</p>
      <span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); border-radius: 50%; display: inline-block; text-align: center;  line-height: 14px;">1</span>
      <span style="margin-right:50px">车辆信息已完善</span>
      <span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); border-radius: 50%; display: inline-block; text-align: center;  line-height: 14px;">2</span>
      <span>绑定司机>=2,且有排班</span>`, '车辆启用', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          dangerouslyUseHTMLString: true
        }).then(async () => {
          const res = await truckStatusUse(row.id)
          // console.log(res)
          this.$message.error(res.msg)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消!'
          })
        })
      }
    }
  }
}
</script>
<style lang="scss">
.el-message-box{
  width:480px;
}
  .customer-list-box{
    padding-left:20px;
    padding-right:20px
  }
  // 搜索区域
  .el-form{
     padding-top:10px;
     .el-form-item{
      margin-bottom:0px;
     }
    .el-form-item__label{
      font-weight: 400;
    }
    .el-button{
      padding:12px 30px;
    }
  }
  // tabs区域
  .tabs{
    margin:20px 0;
    background:white;
  }
    .el-tabs__header{
      margin: 0px;
    }
    .el-tabs--card>.el-tabs__header{
      border:none;
    }
    .el-tabs--card>.el-tabs__header .el-tabs__nav{
      border:none;
    }
    .el-tabs--card>.el-tabs__header .el-tabs__item{
      border-left:none;
      text-align:center;
      padding:10px 40px 10px 40px;
      height:100%;
    }
    .el-tabs--top.el-tabs--card>.el-tabs__header .el-tabs__item:nth-child(2){
      padding-left: 40px;
    }
    .el-tabs--top.el-tabs--card>.el-tabs__header .el-tabs__item:last-child{
      padding-right:40px;
    }
    .el-tabs--card>.el-tabs__header .el-tabs__item.is-active {
      color:#e15537;
      background:#ffeeeb;
      border-bottom: none;
    }
  // 表格区域
  .el-link{
    padding:0 8px;
    line-height: 1;
  }
  // 分页器
  .el-pagination{
    margin-top:20px;
    margin-bottom:70px;
    text-align:center;
    .el-pager li.active{
      color:#e15537;
    }
    .el-pagination__sizes .el-input .el-input__inner:hover{
      border-color:#e15537;
    }
  }
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
}
.vehicle /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

.vehicle {
  /deep/ .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  /deep/ .el-dialog__body {
    text-align: center;
    padding: 20px 20px 0px 20px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}

// 下拉框
/deep/ .el-select {
  width: 100%;
}
</style>

<style>
  .startconfirm{
    width: 481px;

    }
    .startconfirm .el-message-box__header{
      padding-bottom: 4px;
    }
    .startconfirm .el-message-box__container{
      margin-bottom: 15px;
    }
    .confirm .el-message-box__status.el-icon-warning{
      left: 70px;
    }
    .confirm.el-message-box__title{
      font-size: 16px;
    }
    .confirm .el-message-box__message p {
      color: #F92D2D;
      margin-top: 5px;
    }
    .confirm .el-message-box__btns{
      text-align: center;
      margin-top: 10px;
    }
</style>
